package leetcode.code0451;

public class Solution {
	public String addStrings(String num1, String num2) {
		StringBuffer ans = new StringBuffer();
		int p1 = num1.length() - 1, p2 = num2.length() - 1, carry = 0;
		while (p1 >= 0 && p2 >= 0) {
			int n1 = num1.charAt(p1--) - '0';
			int n2 = num2.charAt(p2--) - '0';
			int num = n1 + n2 + carry;
			int cur = num % 10;
			carry = num / 10;
			ans.append(cur);
		}
		while (p1 >= 0) {
			int n1 = num1.charAt(p1--) - '0';
			int num = n1 + carry;
			int cur = num % 10;
			carry = num / 10;
			ans.append(cur);
		}
		while (p2 >= 0) {
			int n2 = num2.charAt(p2--) - '0';
			int num = n2 + carry;
			int cur = num % 10;
			carry = num / 10;
			ans.append(cur);
		}
		if (carry > 0) {
			ans.append(carry);
		}
		return ans.reverse().toString();
	}
}
